package com.ihr360.rest.filter;

import com.ihr360.commons.constant.Constants;
import com.ihr360.commons.context.session.Ihr360Session;
import com.ihr360.commons.context.session.Ihr360SessionContextHolder;
import com.ihr360.commons.entity.AuthUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.RequestContextFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * Created by davidwei on 17-7-17.
 */
@Component
public class IHRRequestFilter extends RequestContextFilter {
    private final static Logger logger = LoggerFactory.getLogger(IHRRequestFilter.class);

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        Ihr360Session session = new Ihr360Session();
        String companyId = request.getHeader(Constants.Session.COMPANY_ID);
        if (StringUtils.isEmpty(companyId)) {
            companyId = request.getParameter(Constants.Session.COMPANY_ID);
        }

        String companyName = request.getHeader(Constants.Session.COMPANY_NAME);
        if (StringUtils.isEmpty(companyName)) {
            companyName = request.getParameter(Constants.Session.COMPANY_NAME);
        }else{
            byte[] bytes = Base64Utils.decodeFromString(companyName);
            companyName = new String(bytes, StandardCharsets.UTF_8);
        }
        String userId = request.getHeader(Constants.Session.USER_ID);
        if (StringUtils.isEmpty(userId)) {
            userId = request.getParameter(Constants.Session.USER_ID);
        }
        String userName = request.getHeader(Constants.Session.USER_NAME);
        if (StringUtils.isEmpty(userName)) {
            userName = request.getParameter(Constants.Session.USER_NAME);
        }else{
            byte[] bytes = Base64Utils.decodeFromString(userName);
            userName = new String(bytes, StandardCharsets.UTF_8);
        }
        String staffId = request.getHeader(Constants.Session.STAFF_ID);
        if (StringUtils.isEmpty(staffId)) {
            staffId = request.getParameter(Constants.Session.STAFF_ID);
        }
        String mobileNo = request.getHeader(Constants.Session.MOBILE_NO);
        if (StringUtils.isEmpty(mobileNo)) {
            mobileNo = request.getParameter(Constants.Session.MOBILE_NO);
        }
        AuthUserInfo info = null;
        if (!StringUtils.isEmpty(companyId)) {
            info = new AuthUserInfo(companyId, companyName, userId, userName, mobileNo, staffId);
        }
        session.setUserInfo(info);
        Ihr360SessionContextHolder.setSession(session);
        super.doFilterInternal(request, response, filterChain);
    }

}
